export default{
	state:{
		// delidx:-1,
		cartList: [{
			id: "10090",
			twoId: 10089,
			name: "拿破仑莓恋",
			french: "Napoléon aux fraises",
			price: "218.00",
			img: "https://static.mcake.com/new_goods/napolunmeilian/N0201/list/1.jpg",
			list:[
				{id: 10090, sku: "n0201", ahead: "提前5小时预定", edible: "2-3人食", spec: "1磅", price: "218.00"},
				{id: 10091, sku: "n0202", ahead: "提前5小时预定", edible: "4-7人食", spec: "2磅", price: "318.00"},
				{id: 10092, sku: "n0203", ahead: "提前5小时预定", edible: "8-12人食", spec: "3磅", price: "458.00"},
				{id: 10093, sku: "n0205", ahead: "提前5小时预定", edible: "12-20人食", spec: "5磅", price: "750.00"}
			],
			ischeck:false,
			num:1, //商品数量
			idx:0 //标志选中的子商品信息
			
		},
		{
			id: "11547",
			twoId: 11540,
			name: "白色恋人",
			french: "Blanc Amant",
			price: "218.00",
			tid: 11,
			ischeck:true,
			num:2, //商品数量
			idx:0,  //标志选中的子商品信息
			tname: "限定",
			list:[
				{id: 10090, sku: "n0201", ahead: "提前5小时预定", edible: "2-3人食", spec: "1磅", price: "218.00"},
				{id: 10091, sku: "n0202", ahead: "提前5小时预定", edible: "4-7人食", spec: "2磅", price: "318.00"},
				{id: 10092, sku: "n0203", ahead: "提前5小时预定", edible: "8-12人食", spec: "3磅", price: "458.00"},
			],
			img: "https://static.mcake.com/new_goods/napolunmeilian/N0201/list/1.jpg"
		}
		]
	},
	getters:{
		allInfo(state){
			let yn = true;
			let csum = 0;
			state.cartList.map(v=>{
				if (!v.ischeck) {
					yn = false;
					return;
				}
				csum += v.list[v.idx].price*v.num;
			});
			return {yn,csum};
		}
	},
	mutations:{
		docheck(state,idx){
			state.cartList[idx].ischeck = !state.cartList[idx].ischeck;
		},
		doall(state,yn){
			state.cartList.map(v=>{
				v.ischeck = !yn;
			})
		},
		dosubmit(state,chgobj){
			let {cartIdx,dropIdx,num} = chgobj;
			
			console.log(cartIdx,dropIdx,num);
			for (var i = 0; i < state.cartList.length; i++) {
				let item = state.cartList[i];
				if (item.id == state.cartList[cartIdx].id && item.idx == dropIdx) {
					state.cartList[i].num +=num;
					state.cartList.splice(cartIdx,1);
					/* state.delidx = cartIdx;
					state.cartList[cartIdx].ischeck = false; */
					return;
				}
			}
			
			state.cartList[cartIdx].num = num;
			state.cartList[cartIdx].idx = dropIdx;
			return;
		},
		addcart(state,addobj){
			// 如果不用响应式，可能会影响数据驱动视图的功能，就是数据变了，页面不变
			// 响应式数据挂载
			/* import Vue from 'vue'
			Vue.set(addobj,'ischeck',true)
			Vue.set(addobj,'num',1) */
			// 非响应式数据数据挂载，数据无法驱动页面，但实测可以，其它端可能有问题？
			addobj.num = 1;
			addobj.ischeck = true;
			
			for (var i = 0; i < state.cartList.length; i++) {
				let item = state.cartList[i];
				if (item.id == addobj.id && item.idx == addobj.idx) {
					state.cartList[i].num++;
					return;
				}
			}
			
			state.cartList.push(addobj);
			uni.showToast({
				title:"加入成功",
				icon:"none"
			})
		},
		delcart(state,idx){
			uni.showModal({
				title:"重要提示",
				content:"确定从购物车中删除？",
				success: (res) => {
					if (res.confirm) {
						state.cartList.splice(idx,1);
					}
				}
			})
			
		},
		delidx(state){
			if (state.delidx>=0) {
				state.cartList.splice(state.delidx,1);
				state.delidx = -1;
			}
		}
	},
	actions:{
		delidxac({commit}){
			commit('delidx');
		}
	}
}